package widget.progress;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ProgressBar;

/**
 * Created by Mr.Fan on 2015/8/10.
 */
public class Progressbar extends ProgressBar implements ProgressInterface {

    private int strokeWidth = 0;
    private int strokeColor = Color.GRAY;
    private int cornerRadius = 0;

    private int backgroundColor = Color.GRAY;
    private int secondaryProgressColor = Color.GRAY;
    private int progressColor = Color.BLUE;

    private ScaleMethod scaleMethod;
    private int duration = 300;
    private boolean showPercentText = true;

    public Progressbar(Context context) {
        this(context, null, android.R.attr.progressBarStyleHorizontal);
    }

    public Progressbar(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.progressBarStyleHorizontal);
    }

    public Progressbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!showPercentText)
            return;
        int width = getWidth() - strokeWidth;
        int height = getHeight() - strokeWidth;
        if (width < 10)
            return;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);
        int textHeight = height - 2;
        Paint.FontMetrics fm = paint.getFontMetrics();
        int fontHeight = (int) (fm.bottom - fm.top);
        if (textHeight > fontHeight)
            textHeight = fontHeight;
        int textSize = textHeight;
        if (textSize < 1)
            return;

        paint.setTextSize(textSize);

        int percent;
        if (isIndeterminate())
            percent = 100 * getIndeterminateDrawable().getLevel() / MAX_LEVEL;
        else
            percent = 100 * getProgress() / getMax();
        String text = percent + " %";
        int charAtX;
        int charAtY = height - (height - textHeight) / 2 - height / textSize - strokeWidth;
        int textColor;
        if (percent < 50) {
            charAtX = width * percent / 100 + 5;
            textColor = ~backgroundColor;
        } else {
            charAtX = width * percent / 100 - (int) paint.measureText(text) - 5;
            textColor = ~progressColor;
        }
        paint.setColor(0xEE000000 | textColor);
        canvas.drawText(text, charAtX, charAtY, paint);
    }

    /**
     * 生成样式
     *
     * @return Drawable
     */
    public Drawable generateDrawable() {
        //设置背景色
        GradientDrawable gdBackground = new GradientDrawable();
        gdBackground.setColor(backgroundColor);
        gdBackground.setStroke(strokeWidth, strokeColor);
        gdBackground.setCornerRadius(cornerRadius);
        //设置第二进度条颜色
        GradientDrawable gdSecondaryProgress = new GradientDrawable();
        gdSecondaryProgress.setColor(secondaryProgressColor);
        gdSecondaryProgress.setStroke(strokeWidth, strokeColor);
        gdSecondaryProgress.setCornerRadius(cornerRadius);
        //设置进度条颜色
        GradientDrawable gdProgress = new GradientDrawable();
        gdProgress.setColor(progressColor);
        gdProgress.setStroke(strokeWidth, strokeColor);
        gdProgress.setCornerRadius(cornerRadius);

        ClipDrawable cdSecondaryProgress = new ClipDrawable(gdSecondaryProgress, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        ClipDrawable cdProgress = new ClipDrawable(gdProgress, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        ProgressLayerDrawable pld = new ProgressLayerDrawable(new Drawable[]{gdBackground, cdSecondaryProgress, cdProgress});
        pld.setDuration(duration);
        //设置id
        pld.setId(0, android.R.id.background);
        pld.setId(1, android.R.id.secondaryProgress);
        pld.setId(2, android.R.id.progress);
        return pld;
    }

    /**
     * 设置边框
     *
     * @param width 边框跨度
     */
    public void setStrokeWidth(int width) {
        strokeWidth = (scaleMethod == null ? width : scaleMethod.scale(width));
    }

    /**
     * 设置边框
     *
     * @param color 边框颜色
     */
    public void setStrokeColor(int color) {
        strokeColor = color;
    }

    /**
     * 设置圆角
     *
     * @param radius 半径
     */
    public void setCornerRadius(int radius) {
        cornerRadius = (scaleMethod == null ? radius : scaleMethod.scale(radius));
    }

    /**
     * 设置背景色
     *
     * @param color 颜色
     */
    public void setBackgroundColor(int color) {
        backgroundColor = color;
    }

    /**
     * 设置第二进度条颜色
     *
     * @param color 颜色
     */
    public void setSecondaryProgressColor(int color) {
        secondaryProgressColor = color;
    }

    /**
     * 设置进度条色
     *
     * @param color 颜色
     */
    public void setProgressColor(int color) {
        progressColor = color;
    }

    @Override
    public void setIndeterminateProgressCompleted() {
        Drawable d = getIndeterminateDrawable();
        if (isIndeterminate() && null != d && d instanceof ProgressLayerDrawable) {
            ((ProgressLayerDrawable) d).setCompleted();
        }
    }

    /**
     * 设置不确定进度条的动画时间
     *
     * @param duration
     */
    public void setIndeterminateAnimationDuration(int duration) {
        this.duration = duration;
    }

    @Override
    public void setShowPercentText(boolean show) {
        this.showPercentText = show;
    }

    @Override
    public void setScaleMethod(ScaleMethod method) {
        scaleMethod = method;
    }

}
